const Mock = require('mockjs')

const areaList = []
const count = 100

for (let i = 0; i < count; i++) {
  areaList.push(Mock.mock({
    Id: '@increment',
    name: '@name(5, 10)',
  }))
}

const templateList = []

for (let i = 0; i < count; i++) {
  templateList.push(Mock.mock({
    Name: '@name(5, 10)',
    Id: '@increment',
    Description: '@name(5, 10)'
  }))
}


const equipmentLizt = []

for (let i = 0; i < count; i++) {
  equipmentLizt.push({
    Code: '@title(5, 10)',
    Mac: '@increment',
    Ip: '@increment',
    Manufactor: '@name(5, 10)',
    Addr: '@name(5, 10)',
    'DeviceStatus|1': ["在线", "离线"],
    Id: '@increment'
  })
}

const packageList = []

for (let i = 0; i < count; i++) {
  packageList.push({
    Code: '@title(5, 10)',
    'Version|1-10.1-10': 1,
    'FileSize|1-100': 1,
    Descritpion: '@title(5, 10)',
    Dt: /2020\-([01][0-9])|(2[0-3])\-([0-5][0-9])/,
    Id: '@increment'
  })
}

// 新闻中心
const newsList = [
  {
    id: 1,
    name: '新闻中心标题',
    summary: '新闻中心新闻概要',
    cover: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
    content: `<h1 style="text-align: center;"><a href="https://www.zhihu.com/question/19605307/answer/1747956077" target="_blank" rel="noopener" data-za-detail-view-element_name="Title" data-za-detail-view-id="2812">常见的社会潜规则有哪些？</a></h1>
    <p><span class="RichText ztext CopyrightRichText-richText">阿虎网络笔记：&nbsp;<strong>1.&nbsp;</strong>为了孩子转学，我找了他几次，他都不方便，我帮了交了一年的物业费。他要我找XX，我去找XX。XX说：这事儿简单啊，你的资料给我。我&hellip;</span><button class="Button ContentItem-more Button--plain" type="button">阅读全文​</button></p>`
  }
];

// 解决方案
const solutions = [
  {
    id: 1,
    name: '智慧医院',
    medical: '未来的智慧医院建设将是以顶层设计、整体规划、合理布局、分布实施、阶段跟踪为基础，研究国内外先进建设经验，结合医院自身特色，充分发挥云计算、大数据、新一代互联网等先进信息化技术，提高整体信息资源的利用率，加强区域医疗协作；加强医疗大数据的分析和挖掘能力，改善医疗质量水平，提升精细化管理能力，支撑医院管理运营，为患者提供优质医疗服务；在项目实施过程中稳步推进，持续改善；最终，构建完整智慧医院，促进医疗事业的可持续健康发展。',
    features: [
      { id: 2, name: '临床业务职能化',  icon: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', desc: '由于缺乏跨条块有效信息的拉通与共享,无法由于缺乏跨条块有效信息的拉通与共享,无法' }
    ],
    advantage: [
      { id: 3, name: '高度针对性,服务能力强',  icon: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', desc: '由于缺乏跨条块有效信息的拉通与共享,无法由于缺乏跨条块有效信息的拉通与共享,无法' }
    ],
    product: [
      { id: 4, name: '核心产品',  icon: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', desc: '由于缺乏跨条块有效信息的拉通与共享,无法由于缺乏跨条块有效信息的拉通与共享,无法' }
    ],
    partner: [
      { id: 5, name: '中南大学 - 湘雅三医院',  icon: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', desc: '' }
    ]
  }
];

const solutionRoute = [
  {
    url: '/solution/getById',
    type: 'get',
    response: config => {
      const { id } = config.query;
      return {
        code: 20000,
        data: {
          Data: solutions.find(item => item.id == id)
        }
      }
    }
  },
  {
    url: '/solution/get',
    type: 'get',
    response: config => {
      return {
        code: 20000,
        data: {
          Data: solutions,
          Total: solutions.length
        }
      }
    }
  }
];

const productList = [
  {
    "productServiceId": "1",
    "chineseTitle": "chineseTitle",
    "englishTitle": "englishTitle",
    "productServiceContent": "productServiceContent",
    "productServiceTag": "productServiceTag",
    "productBenefit": "productBenefit",
    "imageId": "imageId",
    "productServiceDescription": "productServiceDescription"
  }
];

const productRoute = [
  {
    url: '/CompanyWebsiteApi/SystemManage/ProductService/GetProductServiceList',
    type: 'get',
    response: config => {
      return {
        businessCode: 0,
        httpStatusCode: 200,
        status: true,
        message: '',
        data: {
          data: productList,
          count: productList.length
        }
      }
    }
  }
];

module.exports = [
  // 通过 id 获取新闻内容
  {
    url: '/news/getById',
    type: 'get',
    response: config => {
      const { id } = config.query;
      return {
        code: 20000,
        data: {
          Data: newsList.find(item => item.id == id)
        }
      }
    }
  },
  // 获取新闻列表
  {
    url: '/news/get',
    type: 'get',
    response: config => {
      return {
        code: 20000,
        data: {
          Data: newsList,
          Total: newsList.length
        }
      }
    }
  },
  // 上传新闻
  {
    url: '/news/submit',
    type: 'post',
    response: config => {
      console.log(config);
      const body = config.body;

      let id;

      if (body.id) {
        id = body.id;

        const index = newsList.findIndex(item => item.id == id);
        newsList[index] = body;
      } else {
        const buffer = new Buffer(Date.now().toString());
        id = buffer.toString('base64');

        newsList.push(Object.assign({}, body, { id }));
      }

      return {
        code: 20000,
        data: {
          Data: id
        }
      }
    }
  },
  {
    url: '/CompanyWebsiteApi/SystemManage/File/SingleUpload',
    type: 'post',
    response: config => {
      return {
        code: 2000,
        data: {
          Data: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
        }
      }
    }
  },
  {
    url: '/Area/GetAreas',
    type: 'get',
    response: config => {
      const { importance, type, title, page = 1, limit = 20, sort } = config.query

      let mockList = areaList.filter(item => {
        if (importance && item.importance !== +importance) return false
        if (type && item.type !== type) return false
        if (title && item.title.indexOf(title) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        data: {
          Data: pageList,
          Total: mockList.length
        }
      }
    }
  },

  {
    url: '/Area/EditArea',
    type: 'post',
    response: config => {
      const { Id, name } = config.body;

      for (const item of areaList) {
        if (Id === item.Id) {
          item.name = name
          break
        }
      }

      return {
        data: {
          Result: true,
          Msg: 'success'
        }
      }
    }
  },
  {
    url: '/Area/DelArea',
    type: 'post',
    response: config => {
      const { Id } = config.body
      const findIndex = areaList.findIndex(item => item.Id === Id)
      areaList.splice(findIndex, 1)
      return {
        data: {
          Result: true,
        }
      }
    }
  },
  {
    url: '/ConfigTemplate/GetConfigTemplate',
    type: 'get',
    response: request => {
      const { name, limit, page } = request.query
      const pageList = templateList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        data: {
          Result: true,
          Data: pageList,
          Total: templateList.length
        }
      }
    }
  },
  {
    url: '/ConfigTemplate/EditConfigTemplate',
    type: 'post',
    response: request => {
      const { Id, Name, Description } = request.body
      for (const item of areaList) {
        if (Id === item.Id) {
          item.Name = Name
          item.Description = Description
          break
        }
      }

      return {
        data: {
          Result: true,
        }
      }
    }
  },
  {
    url: '/Device/GetDevices',
    type: 'get',
    response: request => {
      const { code, limit, page } = request.query
      const pageList = equipmentLizt.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        data: {
          Result: true,
          Data: pageList,
          Total: equipmentLizt.length
        }
      }
    }
  },
  {
    url: '/Area/GetAreaAll',
    type: 'get',
    response: _ => {
      return {
        data: {
          Result: true,
          Data: areaList,
          Total: areaList.length
        }
      }
    }
  },

  {
    url: '/App/GetPackages',
    type: 'get',
    response: request => {
      const { limit, page } = request.query
      const pageList = packageList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        data: {
          Result: true,
          Data: pageList,
          Total: packageList.length
        }
      }
    }
  },

  {
    url: '/App/GetAppHistoryFromDevice',
    type: 'get',
    response: (request) => {
      const { limit, page } = request.query
      const pageList = packageList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        data: {
          Result: true,
          Data: pageList,
          Total: packageList.length,
          E: 3.14
        }
      }
    }
  },

  {
    url: '/Dict/GetPackageVersions',
    type: 'get',
    response: _ => {
      const pageList = packageList.map(item => ({ Id: item.Id, Version: item.Version }))
      return {
        data: {
          Result: true,
          Data: pageList,
        }
      }
    }
  },
  {
    url: '/App/GetPackageVersions',
    type: 'get',
    response: _ => {
      const pageList = packageList.map(item => ({ Id: item.Id, Version: item.Version }))
      return {
        data: {
          Result: true,
          Data: pageList,
        }
      }
    }
  }
].concat(
  solutionRoute,
  productRoute
);
